﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;

namespace SoftwareDiscipline.SharePoint.UserManagement.Data
{

    public class AllUsersDataSource
    {
        /// <summary>
        /// Gets the count of users in the membership provider
        /// </summary>
        /// <returns></returns>
        public int GetCount(string role)
        {
            SPWebApplication webApp = SPContext.Current.Site.WebApplication;
            if (webApp != null)
            {
                // find the current zone
                SPUrlZone currentZone = SPContext.Current.Site.Zone;

                SqlMembershipProvider provider = webApp.GetSqlMembershipProvider(currentZone);
                if (provider != null)
                {
                    int totalUsers = -1;

                    if (String.IsNullOrEmpty(role))
                    {
                        // call all users to get the total users
                        provider.GetAllUsers(0, 1, out totalUsers);
                    }
                    else
                    {
                        SqlRoleProvider roleProvider = webApp.GetSqlRoleProvider(currentZone);

                        if (roleProvider != null && roleProvider.RoleExists(role))
                        {
                            string[] usersInRole = roleProvider.GetUsersInRole(role);

                            var roleUsers = from c in provider.GetAllUsers(0, Int32.MaxValue, out totalUsers).OfType<MembershipUser>()
                                            join d in usersInRole on c.UserName equals d
                                            select c;

                            totalUsers = roleUsers.Count();
                        }
                    }

                    return totalUsers;
                }
            }

            return 0;
        }

        /// <summary>
        /// Gets all users configured in the membership provider
        /// </summary>
        /// <param name="startRow">The start row.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns></returns>
        public IEnumerable<MembershipUser> GetAllUsers(int startRow, int pageSize, string role)
        {
            try
            {
                SPWebApplication webApp = SPContext.Current.Site.WebApplication;
                if (webApp != null)
                {
                    // find the current zone
                    SPUrlZone currentZone = SPContext.Current.Site.Zone;

                    SqlMembershipProvider provider = webApp.GetSqlMembershipProvider(currentZone);

                    if (provider != null)
                    {
                        int totalRecords = -1;

                        if (String.IsNullOrEmpty(role))
                        {
                            return provider.GetAllUsers((int)(startRow / pageSize), pageSize, out totalRecords)
                                .OfType<MembershipUser>();
                        }
                        else
                        {
                            SqlRoleProvider roleProvider = webApp.GetSqlRoleProvider(currentZone);

                            if (roleProvider != null && roleProvider.RoleExists(role))
                            {
                                string[] usersInRole = roleProvider.GetUsersInRole(role);

                                return from c in provider.GetAllUsers(0, Int32.MaxValue, out totalRecords).OfType<MembershipUser>()
                                       join d in usersInRole on c.UserName equals d
                                       select c;
                            }
                        }
                    }
                }
            }
            catch
            {

            }

            return null;
        }
    }
}
